package com.example.gobang.game;

import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import java.util.concurrent.ConcurrentHashMap;

/**
 * Description: 管理游戏大厅和游戏房间的用户状态
 * Date: 2024-09-05
 * Time: 10:43
 */
@Component
public class OnlineUserManager {
    // 表示用户在游戏大厅的在线状态
    ConcurrentHashMap<Integer, WebSocketSession> gameHall = new ConcurrentHashMap<>();
    // 这个表示当前用户在游戏房间的在线状态
    ConcurrentHashMap<Integer, WebSocketSession> gameRoom = new ConcurrentHashMap<>();

    public void enterGameHall(int userId,WebSocketSession webSocketSession) {
        gameHall.put(userId,webSocketSession);
    }

    /**
     * 离开游戏大厅，状态变为离线
     * @param userId
     */
    public void exitGameHall(int userId) {
        gameHall.remove(userId);
    }

    /**
     * 获取某位用户的在线状态
     * @param userId
     * @return
     */
    public WebSocketSession getFromGameHall(int userId) {
        return gameHall.get(userId);
    }

    public void enterGameRoom(int userId,WebSocketSession webSocketSession) {
        gameRoom.put(userId,webSocketSession);
    }

    public void exitGameRoom(int userId) {
        gameRoom.remove(userId);
    }

    public WebSocketSession getFromGameRoom(int userId) {
        return gameRoom.get(userId);
    }


}
